package com.shushan.db;

import android.content.Context;
import android.util.Log;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.shushan.login.bean.ChildBookBean;
import com.shushan.util.ToolUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: ChildBookDao
 * @Desciption: 定义ChildBookBean数据访问对象，对指定的表进行增删改查操作
 * @author: yichaohua
 * @date: 2018-08-17
 */
public class ChildBookDao {

    private static final String TAG = "ChildBookDao";

    private Dao<ChildBookBean, Integer> childBookDao;
    private DBHelper dbHelper;

    /**
     * 构造方法
     * 获得数据库帮助类实例，通过传入Class对象得到相应的Dao
     * @param context
     */
    public ChildBookDao(Context context) {
        try {
            dbHelper = DBHelper.getHelper(context);
            childBookDao = dbHelper.getDao(ChildBookBean.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        if(childBookDao == null){
            Log.e(TAG,"ChildBookBean == null");
        }
    }

    /**
     * 添加一条记录
     * @param childBookBean
     */
    public void add(ChildBookBean childBookBean) {
        try {
            childBookDao.create(childBookBean);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除一条记录
     * @param childBookBean
     */
    public void delete(ChildBookBean childBookBean) {
        try {
            childBookDao.delete(childBookBean);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void deleteSubjectId(int subjectId) {
        DeleteBuilder deleteBuilder = childBookDao.deleteBuilder();
        try {
            deleteBuilder.where().eq("subject_id", subjectId);
            deleteBuilder.delete();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    /**
     * 更新一条记录
     * @param childBookBean
     */
    public void update(ChildBookBean childBookBean) {
        try {
            childBookDao.update(childBookBean);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询一条记录
     * @param id
     * @return
     */
    public ChildBookBean queryForId(int id) {
        ChildBookBean childBookBean = null;
        try {
            childBookBean = childBookDao.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return childBookBean;
    }

    /**
     * 查询所有记录
     * @return
     */
    public List<ChildBookBean> queryForAll() {
        List<ChildBookBean> childBeanList = new ArrayList<ChildBookBean>();
        try {
            childBeanList = childBookDao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return childBeanList;
    }

    public boolean contain(int id){
        List<ChildBookBean> childBeanList = queryForAll();
        for (ChildBookBean childBean : childBeanList){
            if(childBean.id == id){
                return true;
            }
        }

        return false;
    }

    public boolean containSubject(int subjectId){
        List<ChildBookBean> childBeanList = queryForAll();
        if(subjectId == 0){
            return !ToolUtils.isListEmpty(childBeanList);
        }

        for (ChildBookBean childBean : childBeanList){
            if(childBean.subject_id == subjectId){
                return true;
            }
        }

        return false;
    }

    public void clearTab(){
        try {
            childBookDao.queryRaw("delete from childbook_table");
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    public void addOrUpdateList(List<ChildBookBean> chilidList){
        if(chilidList == null){
            return;
        }

        for (ChildBookBean childBookBean : chilidList){
            if(contain(childBookBean.id)){
                update(childBookBean);
            }else {
                add(childBookBean);
            }
        }
    }

    public void dropAndAddList(List<ChildBookBean> chilidList){
        if(chilidList == null || chilidList.size() == 0){
            return;
        }

        clearTab();

        addOrUpdateList(chilidList);

    }
}
